package cn.huiyunche.base.service.interfaces.impl;

import cn.huiyunche.base.service.interfaces.BCompanyUserService;
import cn.huiyunche.base.service.interfaces.UserService;
import cn.huiyunche.base.service.mappers.BCompanyUserMapper;
import cn.huiyunche.base.service.model.BCompanyUser;
import cn.huiyunche.base.service.model.BCompanyUserExample;
import cn.huiyunche.base.service.vo.UserShowVo;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @FileName: cn.huiyunche.service.base.interfaces.impl
 * @Description: Description
 * @author: Aaron
 * @date: 2016/10/27 下午1:53
 */
@Service
public class BCompanyUserServiceImpl implements BCompanyUserService {

    private static final Logger LOGGER = LoggerFactory.getLogger(BCompanyUserServiceImpl.class);

    @Autowired
    private BCompanyUserMapper bCompanyUserMapper;

    @Autowired
    private UserService userService;

    @Override
    public Long getCompanyIdByUserId(Long userId) {
        LOGGER.info("BCompanyUserServiceImpl.getCompanyIdByUserId param : {}", userId);
        BCompanyUser companyByUserId = this.getCompanyByUserId(userId);
        return null == companyByUserId ? null : companyByUserId.getCompanyId();
    }

    @Override
    public BCompanyUser getCompanyByUserId(Long userId) {
        LOGGER.info("BCompanyUserServiceImpl.getCompanyByUserId param : {}", userId);
        if (null == userId) {
            LOGGER.error("BCompanyUserServiceImpl.getCompanyByUserId param userId must not be null");
            throw new IllegalArgumentException("用户id不能为空");
        }

        BCompanyUserExample example = new BCompanyUserExample();
        example.createCriteria().andUserIdEqualTo(userId);
        List<BCompanyUser> list = bCompanyUserMapper.selectByExample(example);
        if (CollectionUtils.isNotEmpty(list)) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public List<BCompanyUser> getListByCompanyId(Long companyId) {
        LOGGER.info("getListByCompanyId param {}", companyId);
        if (null == companyId) {
            LOGGER.error("getListByCompanyId param companyId must not be null");
            throw new IllegalArgumentException("企业id不能为空");
        }

        BCompanyUserExample example = new BCompanyUserExample();
        example.createCriteria().andCompanyIdEqualTo(companyId);
        return bCompanyUserMapper.selectByExample(example);
    }

    @Override
    public List<BCompanyUser> getListByDivisionId(Integer divisionId) {
        LOGGER.info("BCompanyUserServiceImpl.getListByDivisionId param {}", divisionId);
        if (null == divisionId) {
            LOGGER.error("BCompanyUserServiceImpl.getListByDivisionId param divisionId must not be null");
            throw new IllegalArgumentException("部门主键不能为空");
        }

        BCompanyUserExample example = new BCompanyUserExample();
        example.createCriteria().andDivisionIdEqualTo(divisionId);
        return bCompanyUserMapper.selectByExample(example);
    }

    @Override
    public Boolean isBaseUser() throws Exception {
        UserShowVo user = this.userService.getCurrentUser();
        if (null == user) {
            LOGGER.error("BCompanyUserServiceImpl.isBaseUser user must not be null");
            throw new IllegalArgumentException("用户主键不能为空");
        }
        BCompanyUserExample example = new BCompanyUserExample();
        example.createCriteria().andUserIdEqualTo(user.getId()).andDivisionIdIsNull();
        List<BCompanyUser> companyUsers = this.bCompanyUserMapper.selectByExample(example);
        if (CollectionUtils.isNotEmpty(companyUsers)) {
            return true;
        }
        return false;
    }

    @Override
    public Boolean isBaseUser(Long userId) throws Exception {
        BCompanyUserExample example = new BCompanyUserExample();
        example.createCriteria().andUserIdEqualTo(userId).andDivisionIdIsNull();
        List<BCompanyUser> companyUsers = this.bCompanyUserMapper.selectByExample(example);
        if (CollectionUtils.isNotEmpty(companyUsers)) {
            return true;
        }
        return false;
    }
}
